pacman::p_load(tmap, ggplot2, tidyverse, sf, spNetwork)Take-home Exercise 1 Report
Importing necessary packages
Since we are plotting histograms and interactive maps, we will need to import tmap and ggplot2 respectively. The files I am importing also need to rely on tidyverse. Other geowrangling and plotting tools like sf and spNetwork are required too.
Network-Constrained Kernel Density Estimation (NKDE) & Analysis
Previously, I have already extracted out the roads for Tampines (roads_tampines), Woodlands (roads_woodlands), Downtown Core (roads_downtown_core) and Central Water Catchment (roads_cwc). However, I am writing this in a separate document, so I will reimport and consolidate the roads and points data.
Importing data
# import
roads_tampines <- read_rds("data/analysis/roads_tampines.rds")
origin_tampines <- read_rds("data/analysis/origin_tampines.rds")
dest_tampines <- read_rds("data/analysis/dest_tampines.rds")
roads_downtown_core <- read_rds("data/analysis/roads_downtown_core.rds")
origin_downtown_core <- read_rds("data/analysis/origin_downtown_core.rds")
roads_woodlands <- read_rds("data/analysis/roads_woodlands.rds")
origin_woodlands <- read_rds("data/analysis/origin_woodlands.rds")
roads_cwc <- read_rds("data/analysis/roads_cwc.rds")
dest_cwc <- read_rds("data/analysis/dest_cwc.rds")
mpsz3414 <- st_read("data/geospatial/mpsz.shp")Reading layer `mpsz' from data source
`C:\guacodemoleh\IS415-GAA\Take-home_Ex\Take-home_Ex01\data\geospatial\mpsz.shp'
using driver `ESRI Shapefile'
Simple feature collection with 332 features and 3 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 103.6057 ymin: 1.158699 xmax: 104.0885 ymax: 1.470775
Geodetic CRS: WGS 84
mpsz3414 <- st_transform(mpsz3414, 3414)
summary(mpsz3414) Name SUBZONE_N PLN_AREA_N geometry
Length:332 Length:332 Length:332 MULTIPOLYGON :332
Class :character Class :character Class :character epsg:3414 : 0
Mode :character Mode :character Mode :character +proj=tmer...: 0
Segmentation & Snapping
To perform NKDE, events like pick-up and drop-off points need to be snapped onto the road network. This will enable the lines to gain the necessary spatial weights based on point events.
Create the line segments for all the road networks of our areas of interest.
Locals often walk up to 80 metres to hop onto their Grab vehicle.
Plot out the network densities
Tampines
Origin KDE
tmap_mode("plot")
tm_shape(kde_origin_tm_bw_raster) +
tm_raster("v") +
tm_layout(legend.position = c("left","bottom"),frame=FALSE)Origin NKDE
unique_types <- unique(st_geometry_type(roads_tampines))
# debugging: filter roads_tampines to keep only linestrings
if ("LINESTRING" %in% unique_types) {
roads_tampines <- st_cast(roads_tampines, "LINESTRING")
} else {
# handle the case when no linestrings are found
stop("No linestrings found in roads_tampines.")
}
lixels_tm <- lixelize_lines(roads_tampines,
80,
mindist=40)
samples_tm <- lines_center(lixels_tm)
densities_tm_origin <- nkde(roads_tampines,
events = origin_tampines,
w = rep(1,nrow(origin_tampines)),
samples = samples_tm,
kernel_name = "quartic",
bw = 300,
div= "bw",
method = "simple",
digits = 1,
tol = 1,
grid_shape = c(1,1),
max_depth = 8,
agg = 5, #we aggregate events within a 5m radius (faster calculation)
sparse = TRUE,
verbose = FALSE)
samples_tm$density_origin <- densities_tm_origin
lixels_tm$density_origin <- densities_tm_origin
samples_tm$density_origin <- samples_tm$density_origin*1000
lixels_tm$density_origin <- lixels_tm$density_origin*1000
tmap_mode("view")
tm_shape(lixels_tm)+
tm_lines(col="density_origin")+
tm_shape(origin_tampines)+
tm_dots(alpha=0.1) +
tm_basemap("OpenStreetMap")
